package com.sunday.pinxinghui.model.order;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.entity.Order;
import com.sunday.pinxinghui.entity.RefundDetail;
import com.sunday.pinxinghui.model.WebViewActivity;
import com.sunday.pinxinghui.network.ApiClient;

import java.math.RoundingMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.sunday.pinxinghui.network.ApiClient.API_URL;
import static com.sunday.pinxinghui.network.ApiClient.CUSTOMER_URL;

/**
 * Created by 刘涛 on 2017/8/20.
 */

public class RefundDetailActivity extends BaseActivity {


    @Bind(R.id.title_view)
    TextView titleView;

    @Bind(R.id.money)
    TextView money;
    @Bind(R.id.reason)
    TextView reason;
    @Bind(R.id.time)
    TextView time;
    @Bind(R.id.txt)
    TextView txt;
    @Bind(R.id.txt_detail)
    TextView txtDetail;
    @Bind(R.id.txt_reason)
    TextView txtReason;

    private Order order;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_refund_detail);
        ButterKnife.bind(this);
        order = (Order) getIntent().getSerializableExtra("order");
        titleView.setText(order.getStatus() == 5 ? "退款详情" : "退货详情");
        txt.setText(order.getStatus() == 5 ? "退款申请正在审核中" : "退货申请正在审核中");
        txtDetail.setText(order.getStatus() == 5 ? "退款明细" : "退货明细");
        txtReason.setText(order.getStatus() == 5 ? "退款原因：" : "退货原因：");
        money.setText(String.format("¥%s", order.getTotalFee().setScale(2, RoundingMode.HALF_UP)));
        getDetail();
    }

    RefundDetail refundDetail;

    private void getDetail() {
        showLoadingDialog(0);
        Call<ResultDO<RefundDetail>> call = ApiClient.getApiAdapter().getCustomerServiceInfo(order.getOrderId());
        call.enqueue(new Callback<ResultDO<RefundDetail>>() {
            @Override
            public void onResponse(Call<ResultDO<RefundDetail>> call, Response<ResultDO<RefundDetail>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                dissMissDialog();
                if (response.body().getCode() == 0) {
                    refundDetail = response.body().getResult();
                    updateView();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }


            @Override
            public void onFailure(Call<ResultDO<RefundDetail>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }

    private int id;

    private void updateView() {
        if (refundDetail == null) {
            return;
        }
        money.setText("¥" + refundDetail.getAmount());
        reason.setText(refundDetail.getReason());
        time.setText(refundDetail.getTime());
        id = refundDetail.getCustomerServiceId();
    }


    @OnClick({R.id.btn_1, R.id.btn_2})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_1:
                //撤销申请
                showLoadingDialog(0);
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setTitle("温馨提示");
                builder.setMessage("确认撤销申请吗？");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        cancelRefund();

                    }
                });
                builder.show();

                break;
            case R.id.btn_2:
                //
                intent = new Intent(mContext, WebViewActivity.class);
                intent.putExtra("title", "客服");
                intent.putExtra("url", CUSTOMER_URL);
                startActivity(intent);
                break;
        }
    }

    private void cancelRefund() {
        Call<ResultDO> call = ApiClient.getApiAdapter().cancelRefund(id);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                dissMissDialog();
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext, "已撤销申请");
                    finish();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }


}
